package com.adb.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.adb.entity.GameData;
import com.jjmmbb.jxSudoku.Comm;
import com.jjmmbb.jxSudoku.R;

public class StageAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<GameData> list;

	public StageAdapter(Context context, ArrayList<GameData> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public GameData getItem(int position) {
		return this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GameData gamedata = list.get(position);
		int av_id = gamedata.getAv_id();
		int state = gamedata.getState();
		int js = gamedata.getTime_keeping();

		String idStr = av_id + "";
		String avName = "";
		if (Comm.isPure) {

		} else {
			avName = gamedata.avName;
		}

		if (av_id < 10) {
			idStr = "00" + av_id;
		} else if (av_id < 100) {
			idStr = "0" + av_id;
		}

		TextView idView, nameView, stateView;
		View view;
		if (null == convertView) {
			view = LayoutInflater.from(this.context).inflate(
					R.layout.item_stage, null);
		} else {
			view = convertView;
		}
		idView = (TextView) view.findViewById(R.id.stage_avid);
		nameView = (TextView) view.findViewById(R.id.stage_avName);
		stateView = (TextView) view.findViewById(R.id.stage_state);
		idView.setText(idStr);
		nameView.setText(avName);

		if (1 == state) {
			// stateStr = "已完成";
			Drawable icon_ok = context.getResources().getDrawable(
					R.drawable.icon_finish);
			icon_ok.setBounds(0, 0, 36, 36);
			idView.setCompoundDrawables(icon_ok, null, null, null);
		} else {
			Drawable icon_ok = context.getResources().getDrawable(
					R.drawable.icon_doing);
			icon_ok.setBounds(0, 0, 36, 36);
			idView.setCompoundDrawables(icon_ok, null, null, null);
		}

		String stateStr = secondFormat(js);
		stateView.setText(stateStr);

		return view;
	}

	public String secondFormat(int time) {
		int minute = time / 60;
		int second = time % 60;
		String str_second = second + "";
		if (10 > second) {
			str_second = "0" + second;
		}
		return minute + ":" + str_second;
	}

}
